import gameMechanics.Card;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class HandCards {
	private ArrayList<Card> tableCards;
	private ArrayList<PlayerHand> playerCards;
	public HandCards(ArrayList<Card> table){
		tableCards = table;
		playerCards = new ArrayList<PlayerHand>();
	}
	public HandCards(String socketRep){
		String table = socketRep.substring(0,15);
		tableCards = new ArrayList<Card>();
		for (int i = 0; i < 5; i++){
			tableCards.add(new Card(Integer.parseInt(table.charAt(1 + (i * 3))+"")));
		}
		playerCards = new ArrayList<PlayerHand>();
		String players = socketRep.substring(15);
		Pattern p = Pattern.compile("(\\{\\{)(.*?)(\\}\\{)(.*?)(\\}\\})");
		Matcher m = p.matcher(players);

		while (m.find()) {
		    //System.out.println(m.group(4));
		    String card1 = m.group(2);
		    String card2 = m.group(4);
		    playerCards.add(new PlayerHand(new Card(Integer.parseInt(card1)),new Card(Integer.parseInt(card2))));
		}
	}
	public void addPlayerHand(PlayerHand ph){
		playerCards.add(ph);
	}
	public ArrayList<Card> getTableCards(){
		return tableCards;
	}
	public ArrayList<PlayerHand> getPlayerCards(){
		return playerCards;
	}
	public String toString(){
		String messageContents = "";
		for (Card card : getTableCards()){
			messageContents += "{"+card.toString()+"}";
		}
		for (PlayerHand hand : getPlayerCards()){
			messageContents += "{{"+ hand.getCard1().toString()+"}";
			messageContents += "{" + hand.getCard2().toString() + "}}";
		}
		return messageContents;
	}
	@Override
	public boolean equals(Object other){
		return this.toString().equals(other.toString());
	}
}
